<?php
class Edit extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/yunying/product/'));
		
		$id = $this->get('id', 'int');
		if(!$id) header('Location:/yunying/product/');
		
		$data = array();
		$tpl = 'yunying/product/info.html';
		
		$mProduct = new Product_model();
		//获取景点信息
		$data['info'] = $mProduct->productInfo(array('id' => $id));
		
		//获取所有分类
		$mProduct = new Product_model();
		$category = $mProduct->category();
		if($category) $_category = $category;
		else  $_category = array();
		$data['category'] = array();
		foreach($_category as $key => $value) {
			$data['category'][$value['cid']] = $value['name'];
		}
		
		$data['act'] = 'edit';
		$this->display($data, $tpl);
	}
	public function json() {
		$id = $this->post('id', 'str');
		$name = $this->post('name', 'str');
		$cid = $this->post('cid', 'int', 0);
		$province = $this->post('province', 'int');
		$location = $this->post('location', 'str');
		$city = $this->post('city', 'int');
		$description = $this->post('description', 'str');
		
		if(!$id) $this->displayJSON(array('status' => -1, 'message' => '请输入景点名称'));
		if(!$name) $this->displayJSON(array('status' => -2, 'message' => '请输入景点名称'));
		if(!$province) $this->displayJSON(array('status' => -3, 'message' => '请选择省份'));
		if(!$city) $this->displayJSON(array('status' => -4, 'message' => '请选择城市'));
		if(!$description) $this->displayJSON(array('status' => -5, 'message' => '请输入描述'));
		
		$mProduct = new Product_model();
		
		//检查产品名称是否存在
		$params = array('name' => $name, 'province' => $province, 'city' => $city, 'id !=' => $id);
		$result = $mProduct->productInfo($params);
		if($result) $this->displayJSON(array('status' => -6, 'message' => '景点名称已存在'));
		
		$params = array('name' => $name, 'cid' => $cid, 'province' => $province, 'city' => $city, 'description' => $description, 'location' => $location);
		$result = $mProduct->edit($id, $params);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '添加景点成功'));
		else $this->displayJSON(array('status' => -7, 'message' => '添加景点失败'));
	}
}